<template>
  <div class="product-card">
    <!-- 商品图片区域 -->
    <div class="product-image">
      <img :src="product.imageUrl" :alt="product.name" class="product-img">
      <div v-if="product.discount" class="discount-tag">{{ product.discount }}</div>
    </div>
    
    <!-- 商品信息区域 -->
    <div class="product-info">
      <!-- 商品标题 -->
      <h3 class="product-name">{{ product.name }}</h3>
      
      <!-- 商品评价 -->
      <div class="product-rating">
        <div class="star-rating" :style="{ width: (product.rating / 5 * 100) + '%' }"></div>
        <span class="rating-text">{{ product.rating }} ({{ product.reviewCount }}评)</span>
      </div>
      
      <!-- 商品价格 -->
      <div class="product-price">
        <span class="current-price">¥{{ product.currentPrice }}</span>
        <span v-if="product.originalPrice" class="original-price">¥{{ product.originalPrice }}</span>
      </div>
      
      <!-- 商品销量 -->
      <div class="product-sales">
        <span>已售 {{ formatNumber(product.sales) }}</span>
      </div>
      
      <!-- 店铺信息 -->
      <div class="shop-info">
        <img :src="product.shopIcon" :alt="product.shopName" class="shop-icon">
        <span class="shop-name">{{ product.shopName }}</span>
      </div>
    </div>
    
    <!-- 操作按钮 -->
    <div class="product-actions">
      <button class="add-to-cart-btn" @click.stop="addToCart">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path d="M3 10H21V20C21 20.5304 20.7893 21.0391 20.4142 21.4142C20.0391 21.7893 19.5304 22 19 22H5C4.46957 22 3.96086 21.7893 3.58579 21.4142C3.21071 21.0391 3 20.5304 3 20V10Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          <path d="M7 10V6C7 4.89543 7.89543 4 9 4H19C20.1046 4 21 4.89543 21 6V10M7 10H3M3 10L3 10.01M3 10L21 10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      </button>
      <button class="buy-now-btn" @click.stop="buyNow">立即购买</button>
    </div>
  </div>
</template>

<script setup>
import { defineProps } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const props = defineProps({
  product: {
    type: Object,
    required: true
  }
});

// 格式化数字
const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万';
  }
  return num.toString();
};

// 加入购物车
const addToCart = () => {
  // 这里可以添加实际的加入购物车逻辑
  console.log('加入购物车:', props.product.name);
  // 显示成功提示
  alert(`${props.product.name} 已成功加入购物车`);
};

// 立即购买
const buyNow = () => {
  // 导航到商品详情页或直接下单
  router.push(`/product/${props.product.id}`);
};
</script>

<style scoped>
.product-card {
  background-color: var(--card-background);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 16px;
  transition: all var(--transition-normal);
  border: 1px solid var(--border-color);
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.product-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-4px);
}

/* 商品图片区域 */
.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  background-color: #f8f8f8;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform var(--transition-normal);
}

.product-card:hover .product-img {
  transform: scale(1.05);
}

.discount-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background-color: #e1251b;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}

/* 商品信息区域 */
.product-info {
  flex: 1;
  min-height: 0;
}

.product-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  margin: 0 0 8px 0;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-rating {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.star-rating {
  height: 12px;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23FFAC33"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>');
  background-repeat: repeat-x;
  background-size: 12px 12px;
  margin-right: 6px;
  flex-shrink: 0;
}

.rating-text {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.product-price {
  display: flex;
  align-items: baseline;
  margin-bottom: 8px;
}

.current-price {
  font-size: 18px;
  font-weight: 700;
  color: #e1251b;
  margin-right: 8px;
}

.original-price {
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: line-through;
}

.product-sales {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.shop-info {
  display: flex;
  align-items: center;
}

.shop-icon {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  margin-right: 6px;
}

.shop-name {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 操作按钮 */
.product-actions {
  display: flex;
  margin-top: 12px;
  gap: 8px;
}

.add-to-cart-btn {
  flex: 0 0 40px;
  height: 40px;
  background-color: #e1251b;
  color: white;
  border: none;
  border-radius: var(--radius-full);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.add-to-cart-btn:hover {
  background-color: #c11f17;
  transform: scale(1.05);
}

.buy-now-btn {
  flex: 1;
  height: 40px;
  background-color: #fff;
  color: #e1251b;
  border: 1px solid #e1251b;
  border-radius: var(--radius-full);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all var(--transition-fast);
}

.buy-now-btn:hover {
  background-color: #fff1f0;
}
</style>